package org.jeecg.modules.inventory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.estar.tw.util.StringUtils;
import org.jeecg.modules.inventory.entity.BussInventoryLedger;
import org.jeecg.modules.inventory.entity.BussInventoryLedgerDetails;
import org.jeecg.modules.inventory.service.IBussInventoryLedgerService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 库存台账
 * @Author: jeecg-boot
 * @Date:   2024-07-01
 * @Version: V1.0
 */
@Api(tags="库存管理")
@RestController
@RequestMapping("/inventory/bussInventoryLedger")
@Slf4j
public class BussInventoryLedgerController {
	@Autowired
	private IBussInventoryLedgerService bussInventoryLedgerService;

	/**
	 * 分页列表查询
	 *
	 * @param bussInventoryLedger
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "库存管理-分页列表查询")
	@ApiOperation(value="库存管理-分页列表查询", notes="库存管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BussInventoryLedger>> queryPageList(BussInventoryLedger bussInventoryLedger,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  HttpServletRequest req) {
		IPage<BussInventoryLedger> pageList = new Page<>();
		//如果没选仓库直接返回空
		if(StringUtils.isEmpty(bussInventoryLedger.getWarehouseId())){
			return Result.OK(pageList);
		}
		Page<BussInventoryLedger> page = new Page<BussInventoryLedger>(pageNo, pageSize);
		pageList = bussInventoryLedgerService.myPage(page, bussInventoryLedger,req.getParameterMap());
		return Result.OK(pageList);
	}
	@ApiOperation(value="库存明细主表ID查询", notes="库存明细-通主表ID查询")
	@GetMapping(value = "/queryBussInventoryLedgerDetailsByMainId")
	public Result<List<BussInventoryLedgerDetails>> queryBussInventoryLedgerDetailsByMainId(@RequestParam(name="warehouseId",required=true) String warehouseId, @RequestParam(name="materialId",required=true) String materialId, @RequestParam(name="yearmonth",required=true) String yearmonth) {
		List<BussInventoryLedgerDetails> bussInventoryDetailsList = bussInventoryLedgerService.selectByMainId(warehouseId,materialId,yearmonth);
		return Result.OK(bussInventoryDetailsList);
	}


	/**
	 * 导出 excel
	 * @param bussInventoryLedger
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@RequiresPermissions("org.jeecg.modules.demo:buss_storage_notice:exportXls")
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(BussInventoryLedger bussInventoryLedger,
								  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								  HttpServletRequest req) {
		// Step.1 组装查询条件查询数据
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		IPage<BussInventoryLedger> pageList = new Page<>();
		//如果没选仓库直接返回空
//		if(StringUtils.isEmpty(bussInventoryLedger.getWarehouseId())){
//			return Result.OK(pageList);
//		}
		Page<BussInventoryLedger> page = new Page<BussInventoryLedger>(pageNo, pageSize);
		pageList = bussInventoryLedgerService.myPage(page, bussInventoryLedger,req.getParameterMap());
		//配置选中数据查询条件
//      String selections = request.getParameter("selections");
//      if(oConvertUtils.isNotEmpty(selections)) {
//         List<String> selectionList = Arrays.asList(selections.split(","));
//         queryWrapper.in("id",selectionList);
//      }
		//Step.2 获取导出数据
		List<BussInventoryLedger> records = pageList.getRecords();
//		IPage<BussStorageNoticePage> bussStorageNoticePageIPage = bussStorageNoticeService.exportList(page, queryWrapper);
//		List<BussStorageNoticePage> records = bussStorageNoticePageIPage.getRecords();
//      List<BussStorageNotice> bussStorageNoticeList = bussStorageNoticeService.list(queryWrapper);

		// Step.3 组装pageList
//      List<BussStorageNoticePage> pageList = new ArrayList<BussStorageNoticePage>();
//      for (BussStorageNoticePage main : records) {
//          BussStorageNoticePage vo = new BussStorageNoticePage();
//          BeanUtils.copyProperties(main, vo);
////          List<BussStorageNoticeCarinfoVo> bussStorageNoticeCarinfoList = bussStorageNoticeCarinfoService.selectByMainId(main.getId());
////          vo.setBussStorageNoticeCarinfoList(BeanPlusUtil.toBeanList(bussStorageNoticeCarinfoList,BussStorageNoticeCarinfo.class) );
////		  List<BussStorageNoticeMaterialVo> bussStorageNoticeMaterialVos = bussStorageNoticeMaterialService.selectByMainId(main.getId());
////		  vo.setBussStorageNoticeMaterialVoList(bussStorageNoticeMaterialVos);
//          pageList.add(vo);
//      }
		// Step.4 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, "库存台账列表");
		mv.addObject(NormalExcelConstants.CLASS, BussInventoryLedger.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("库存台账数据", "导出人:"+sysUser.getRealname(), "入库通知单"));
		mv.addObject(NormalExcelConstants.DATA_LIST, records);
		return mv;
	}

}
